using System;

namespace Microsoft.SqlServer.Management.SqlManagerUI.Scripting
{
	internal class ObjectScriptHandler : ScriptHandler
	{
		private string objectType;

		private ScriptActions scriptType;

		private ScriptedObjectCollection objectsScripted = new ScriptedObjectCollection();

		public ObjectScriptHandler(string objectType, ScriptActions scriptType) : this(objectType, scriptType, null)
		{
		}

		public ObjectScriptHandler(string objectType, ScriptActions scriptType, ScriptHandler successor) : base(successor)
		{
			this.objectType = objectType;
			this.scriptType = scriptType;
		}

		protected override bool HandleScriptedEventCore(ObjectScriptedEventArgs e)
		{
			if ((this.objectType != null && e.ObjectInfo.Urn.Type != this.objectType) || (this.objectType == null && e.ObjectInfo.Urn.Type == "DdlTrigger"))
			{
				return false;
			}
			if (this.scriptType == ScriptActions.None || (this.scriptType & e.Action) == ScriptActions.None)
			{
				return false;
			}
			this.objectsScripted.AddScriptFragment(e.ObjectInfo, e.Script, e.Action);
			return true;
		}

		protected override void ScriptCore(ScriptWriter scriptWriter)
		{
			this.objectsScripted.ScriptAll(scriptWriter);
		}
	}
}
